package seekting.demo2017.retrofit;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by tony on 2017/8/1.
 */

public final class HttpGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private static final Charset UTF_8 = Charset.forName("UTF-8");
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    public HttpGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        JSONObject json = null;
        try {
            json = new JSONObject(value.string());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            return adapter.fromJson(json.optString("T1348647909107").toString());
        } finally {
            value.close();
        }
    }
}
